package com.search.api;

import com.base.model.R;
import com.search.model.Media;
import com.search.service.MediaSearchService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.List;

/**
 * @Author: 小何
 * @Description: TODO
 * @DateTime: 2023/9/26 14:40
 **/
@RestController
@RequestMapping("/search")
public class MediaSearchController {

    @Autowired
    private MediaSearchService mediaSearchService;

    /**
     * 搜索框查询
     *
     * @param searchContext 搜索框内容
     * @param orderCode     排序码
     * @param page          页码,页面大小固定60
     * @param category      分类
     * @param timeCode      时间码,通过时间码区分时间过滤条件上下限。例如时间码为1,代表查找视频时长10分钟以下的视频,
     *                      即上限为10,下限为0
     * @return 返回视频信息
     */
    @PostMapping("/listBySearchBox")
    public R<List<Media>> listMediaBySearchBox(@RequestParam("searchContext") String searchContext, @RequestParam(value = "orderCode",required = false) Integer orderCode, @RequestParam(value = "page",required = false) Integer page, @RequestParam(value = "category",required = false) Integer category, @RequestParam(value = "timeCode",required = false) Integer timeCode) {
        return mediaSearchService.listMediaBySearchBox(searchContext, orderCode, page, category, timeCode);
    }


    /**
     * 通过视频Id删除es文档
     *
     * @param mediaId
     * @return
     */
    @DeleteMapping("/deleteMedia")
    public R<String> deleteMediaByMediaId(@RequestParam("mediaId") String mediaId) {
        try {
            return mediaSearchService.deleteMediaByMediaId(mediaId);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return R.error("文档删除失败");
    }


    @PutMapping("/addMedia")
    public R<String> saveMedia(@RequestBody Media media){
        return mediaSearchService.saveMedia(media);
    }



}
